﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.Configuration;

namespace qdata
{
    public partial class GetOrgData : Form
    {
        public GetOrgData()
        {
            InitializeComponent();
            comboBox1.Items.Add("UTF-8");
            comboBox1.Items.Add("GB2312");
            cmbType.Items.Add("火车信息");
            cmbType.Items.Add("汽车信息");
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
          

            string url = "http://www.huoche.net/lieche/zhanzhan.php?k1={0}&k2={1}";
            GetCarStation s = new GetCarStation();
           
            url = string.Format(url, txtscity.Text, txtecity.Text);
            huochenet.GetInstance().SaveHuocheData(url,txtsite.Text  );
            MessageBox.Show("已成功"); 
        }

        private string Getstr(string s)
        {
            Regex re = new Regex("</?.+?>");
            string result = re.Replace(s,"");
            return result;
        }


        string connectionString = ConfigurationSettings.AppSettings["connectionstring"].ToString();
        /// <summary>
        /// 执行SQL,返回DataTable
        /// </summary>
        /// <param name="Sql">SQL</param>
        /// <returns></returns>
        public   DataTable ExecuteDataTable(string Sql)
        {

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {
                    string sqlCommand = Sql;
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();
                        OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                        adp.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        adp.Fill(ds);
                        return ds.Tables[0];
                    }
                }
                catch (Exception ex)
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    return null;
                }
            }
        }
 

        private void button3_Click(object sender, EventArgs e)
        {

            
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] SarCityArr=null ;
            string[] EndCityArr = null;
            if (txtscity.Text == string.Empty)
                return;
            if (txtecity.Text == string.Empty)
                return;
            SarCityArr = txtscity.Text.Split(',');

            EndCityArr = txtecity.Text.Split(',');

            if (cmbType.Text == "火车信息")
            {
                string url = "http://www.huoche.net/lieche/zhanzhan.php?k1={0}&k2={1}";
                GetCarStation s = new GetCarStation();
                
                
             
      
                foreach (string s1 in SarCityArr)
                {
                    foreach (string s2 in EndCityArr)
                    {

                        huochenet.GetInstance().SaveHuocheData(string.Format(url, s1, s2),txtsite.Text );
                    }
                }

                MessageBox.Show("已成功");
            }
            else if (cmbType.Text == "汽车信息")
            {
                string url = " http://www.keyunzhan.com/zhandaozhan_search.php?find_type=2&shousuo=2&startstate={0}&endstate={1}";
                url = string.Format(url, txtscity.Text, txtecity.Text);
            
                foreach (string s1 in SarCityArr)
                {
                    foreach (string s2 in EndCityArr)
                    {
                        KyzData.GetInstance().SaveCarstation(string.Format(url, s1, s2), s1, s2,txtsite.Text );
                        
                    }
                }
                MessageBox.Show("已成功");

            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            doData gdata = new doData();
            gdata.Show();
        }
    }
}
